package com.hezhi.project.module.teacher.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hezhi.project.module.teacher.domain.Teacher;
import com.hezhi.project.module.teacher.service.ITeacherService;
import com.hezhi.framework.web.controller.BaseController;
import com.hezhi.framework.web.page.TableDataInfo;
import com.hezhi.framework.web.domain.AjaxResult;

/**
 * 教师 信息操作处理
 * 
 * @author hezhi
 * @date 2018-08-17
 */
@Controller
@RequestMapping("/module/teacher")
public class TeacherController extends BaseController
{
    private String prefix = "module/teacher";
	
	@Autowired
	private ITeacherService teacherService;
	
	@RequiresPermissions("module:teacher:view")
	@GetMapping()
	public String teacher()
	{
	    return prefix + "/teacher";
	}
	
	/**
	 * 查询教师列表
	 */
	@RequiresPermissions("module:teacher:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Teacher teacher)
	{
		startPage();
        List<Teacher> list = teacherService.selectTeacherList(teacher);
		return getDataTable(list);
	}
	
	/**
	 * 新增教师
	 */
	@RequiresPermissions("module:teacher:add")
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}

	/**
	 * 修改教师
	 */
	@RequiresPermissions("module:teacher:edit")
	@GetMapping("/edit/{teacherNumber}")
	public String edit(@PathVariable("teacherNumber") String teacherNumber, Model model)
	{
		Teacher teacher = teacherService.selectTeacherById(teacherNumber);
		model.addAttribute("teacher", teacher);
	    return prefix + "/edit";
	}
	
	/**
	 * 保存教师
	 */
	@RequiresPermissions("module:teacher:save")
	@PostMapping("/save")
	@ResponseBody
	public AjaxResult save(Teacher teacher)
	{
		if (teacherService.saveTeacher(teacher) > 0)
		{
			return success();
		}
		return error();
	}
	
	/**
	 * 删除教师
	 */
	@RequiresPermissions("module:teacher:remove")
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{
		int rows = teacherService.deleteTeacherByIds(ids);
		if (rows > 0)
        {
            return success();
        }
        return error();
	}
	
	
	/**
	 * 验证教师工号唯一
	 * @param grade
	 * @return
	 */
	@PostMapping("/checkTeacherNumber")
	@ResponseBody
	public String checkTeacherNumber(Teacher teacher){
		if(teacher != null && !"".equals(teacher.getTeacherNumber()) && teacher.getTeacherNumber() != null){
			if(teacherService.selectTeacherById(teacher.getTeacherNumber())!=null){
				return "1";
			}
		}
		return "0";
	}
	
}
